var express = require('express');
const mysql = require("mysql");
var file = require('fs')
const zhaopin = express();
// var multer = require("multer")

//设置跨域访问
zhaopin.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next()
});

var connsql = mysql.createConnection({
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: 'root',
  database: 'zhaopin' // 数据库
})
connsql.connect() // 开启与数据库连接
console.log("数据库连接成功")

//接受传递过来的参数
const bodyParser = require('body-parser');
zhaopin.use(bodyParser.urlencoded({ extended: false }));
zhaopin.use(bodyParser.json());


/* GET home page. */
// router.get('/', function(req, res, next) {
//   res.render('index', { title: 'Express' });
// });

zhaopin.get('/', function (req, res, next) {
  var selsql = "select * from admin"
  connsql.query(selsql, (err, result) => {
    if (err) {
      console.log(err);
      return
    }
    else {
      res.send(result)
    }
  })
});



module.exports = zhaopin;
zhaopin.listen(3080, () => {
  console.log("启动3080");
})

